package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.ImagesDao;
import com.woniuxy.dao.ProductionDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.ProductionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author Leimin
 * @date 2022/04/27 14:51
 **/
@Service
public class ProductionServiceImpl implements ProductionService {
    @Resource
    ProductionDao productionDao;
    @Resource
    ImagesDao imagesDao;

    @Override
    public ResponseEntity findAllByCondition(QueryEntity<Production> queryEntity) {
        PageHelper.startPage(queryEntity.getCurrentPage(),queryEntity.getPageSize());

        String orderSort = queryEntity.getOrderSort();
        if("asc".equalsIgnoreCase(orderSort) || "desc".equalsIgnoreCase(orderSort)){
            PageHelper.orderBy(queryEntity.getOrderKey()+" "+orderSort);
        }

        List<Production> list = productionDao.findAllByCondition(queryEntity.getEntity());
        PageInfo<Production> pageInfo = PageInfo.of(list);
        return new ResponseEntity<>(200,"成功",pageInfo);
    }

    @Override
    public ResponseEntity findAll() {
        List<Production> list = productionDao.findAll();
        return new ResponseEntity(200,"成功",list);
    }

    @Override
    public ResponseEntity findOne(int id) {
        Production production = productionDao.findOne(id);
        return new ResponseEntity(200,"成功",production);
    }

    @Transactional
    @Override
    public ResponseEntity addOne(Production production) {
        //插入产品表
        productionDao.addOne(production);
        //插入仓库中间表
        productionDao.addToWarehouse(production.getMaterial_code(), production.getWarehousename());
        //插入图片表
        if(production.getImages().size()>0){
            imagesDao.addBatch(production.getImages());
        }

        return ResponseEntity.SUCCESS;
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        int index = productionDao.deleteOne(id);
        if (index>0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity deleteBatch(int[] ids) {
        int index = productionDao.deleteBatch(ids);
        if (index>0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Transactional
    @Override
    public ResponseEntity updateOne(Production production) {
        //查询原来的商品
        Production oldProduct= productionDao.findOne(production.getId());
        //更新产品表
        productionDao.updateOne(production);
        //更新中间表
        productionDao.updateWarehouse(production.getMaterial_code(), production.getWarehousename());
        //更新图片表
        if(!oldProduct.getMaterial_code().equals(production.getMaterial_code())
                && production.getImages().size()>0){
            imagesDao.updateBatch(production.getImages(),production.getMaterial_code());
        }
        return ResponseEntity.SUCCESS;
    }

    @Override
    public ResponseEntity findSelectOption() {
        List<SelectOption> typeOption = productionDao.findTypeOption();
        List<SelectOption> brandOption = productionDao.findBrandOption();
        List<SelectOption> unitOption = productionDao.findUnitOption();
        List<SelectOption> warehouseOption = productionDao.findWarehouseOption();
        HashMap<String, List> map = new HashMap<>();
        map.put("typeOption",typeOption);
        map.put("brandOption",brandOption);
        map.put("unitOption",unitOption);
        map.put("warehouseOption",warehouseOption);
        return new ResponseEntity(200,"成功",map);
    }

    @Override
    public ResponseEntity findByCode(String material_code) {
        Production production = productionDao.findByCode(material_code);
        if (production!=null){
            return new ResponseEntity(200,"成功",production);
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity findByName(String name) {
        Production production = productionDao.findByName(name);
        if (production!=null){
            return new ResponseEntity(200,"成功",production);
        }
        return ResponseEntity.Fail;
    }
}

